package cj.web.marry.controllers;

import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;

import cj.utils.Constants;
import cj.utils.ReturnUUID;
import cj.utils.sms.SendSMSUtil;
import cj.web.marry.dao.AppTokenDAO;

@Path("apptoken")
public class AppTokenController {
	public final static String tempid="102126";//模版ID
	public final static String params2="1";//时间参数
	public final static String accountSid="8aaf0708559f32dd0155a061b0e70089";
	public final static String accountToken="ca8d98f8170b4293be592df629a8ea07";
	public final static String appId="8aaf070855ed9ef10155fd08aee90737";

	@Autowired
	private AppTokenDAO appTokenDao; 
	
	/**
	 * 发送验证码
	 * @param phone	手机号
	 * @return
	 */
	@Get("sendToken")
	public String createLoginToken(@Param("phone") String phone) {
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		//验证码
		String token= ReturnUUID.getRandomNum();
		//组合为数组传递
		String[] msg = new String[]{token,params2};
		//调用短信平台
		String rep = SendSMSUtil.sendSMS(accountSid, accountToken, appId, phone, tempid, msg);
		if(rep.equals(Constants.returnObj.returnFlag_true))
		{
			appTokenDao.updateToken(phone);
			appTokenDao.createToken(phone, token);
		}
		else
		{
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@"+jo.toString();	
	}
}
